#!/usr/bin/env python
# coding=utf-8
from django.core.serializers.json import DjangoJSONEncoder
from datetime import datetime
import time,json



def handle_uploaded_file(file,name_in_tmp_dir):
    '''存储文件file到系统tmp文件夹，并命名为name_in_tmp_dir'''
    path = '/tmp/'+name_in_tmp_dir
    with open(path, 'wb') as xls:
        for chunk in file.chunks():
            xls.write(chunk)


def handle_time_string(time_string, style):
    '''将格式化时间字符串转换为datetime对象'''
    strp_time = time.strptime(time_string, style)
    return datetime.fromtimestamp(time.mktime(strp_time))


def phone_json(QRinfo, GETinfo):
    '''包装成json发送，移动设备app通讯用'''
    QRinfo = {
        'QRinfo': QRinfo,
        'GETinfo': GETinfo
    }
    return json.dumps(QRinfo, ensure_ascii=False, cls=DjangoJSONEncoder)

